package com.xhwl.logistics.component;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import com.alibaba.fastjson.JSONObject;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.dto.ResponseCommonPushCoSonDTO;
import com.xhwl.logistics.dto.http.query.QueryCommonCoSonDTO;
import com.xhwl.logistics.dto.push.CommonPushCoSonDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Map;


@Component
@Slf4j
/**
 * 科松车场签名算法
 */
public class SignForCoSon {

    public <T extends QueryCommonCoSonDTO> T httpSign(T t, String appId, String parkKey, String version,VehicleParkingInfoBO vehicleParkingInfoBO) {
        t.setAppid(appId);
        t.setParkKey(parkKey);
        t.setVersion(version);
        double a = Math.random() * 10;
        t.setRand(String.format("%.9f", a));
        Map<String, Object> map = BeanUtil.beanToMap(t, false, true);
        map.entrySet().removeIf(entry -> "".equals(entry.getValue()));
        String[] sKeys = map.keySet().toArray(new String[]{});
        Arrays.sort(sKeys);
        StringBuilder s1 = new StringBuilder();
        for (String k :sKeys){
            map.put(k,map.get(k));
            s1.append(k).append("=").append(map.get(k)).append("&");
        }
        s1.deleteCharAt(s1.length()-1);
        String value = SecureUtil.signParams(DigestAlgorithm.MD5, map, "&", "=", true, "&", vehicleParkingInfoBO.getAppSecret()).toUpperCase();
        log.info(value);
        t.setSign(value);
        return t;
    }

    public ResponseCommonPushCoSonDTO sign(ResponseCommonPushCoSonDTO responseCommonPushCoSonDTO, String code, String msg, VehicleParkingInfoBO vehicleParkingInfoBO) {
        responseCommonPushCoSonDTO.setCode(code);
        responseCommonPushCoSonDTO.setMsg(msg);
        double a = Math.random() * 10;
        responseCommonPushCoSonDTO.setRand(String.format("%.9f", a));
        Map<String, Object> map = BeanUtil.beanToMap(responseCommonPushCoSonDTO, false, true);
        map.entrySet().removeIf(entry -> "".equals(entry.getValue()));
        responseCommonPushCoSonDTO.setSign(SecureUtil.signParams(DigestAlgorithm.MD5, map, "&", "=", true, "&", vehicleParkingInfoBO.getAppSecret()).toUpperCase());
        return responseCommonPushCoSonDTO;
    }

    /**
     *校验科松厂商sign值
     */
    public <T extends CommonPushCoSonDTO> ResponseCommonPushCoSonDTO checkSign(T t, String AppSecret) {
        String value = t.getSign();
        Map<String, Object> map = BeanUtil.beanToMap(t, false, true);
        //删除空字段和sign,科松签名算法需要。
        map.entrySet().removeIf(entry -> "".equals(entry.getValue()));
        map.entrySet().removeIf(entry -> value.equals((entry.getValue())));

        String[] sKeys = map.keySet().toArray(new String[]{});
        Arrays.sort(sKeys);
        StringBuilder s1 = new StringBuilder();
        for (String k :sKeys){
            map.put(k,map.get(k));
            if(map.get(k) == t.getData()){
                String s = JSONObject.toJSONString(map.get(k));
                map.put(k,s);
            }
            s1.append(k).append("=").append(map.get(k)).append("&");
        }
        s1.deleteCharAt(s1.length()-1);
        log.info(SecureUtil.signParams(DigestAlgorithm.MD5, map, "&", "=", true, "&", AppSecret).toUpperCase());
        if (value.equals (SecureUtil.signParams(DigestAlgorithm.MD5, map, "&", "=", true, "&", AppSecret).toUpperCase())){
            return null;
        }
        ResponseCommonPushCoSonDTO response = new ResponseCommonPushCoSonDTO();
        response.setCode("0");
        response.setMsg("签名错误");
        response.setRand(null);
        response.setSign(null);
        return response;

    }

}
